package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;


    @RequestMapping("/upload")
    private Result upload(@RequestParam("excelFile") MultipartFile excelFile) throws IOException {
        //使用poi对文件进行读取
        List<String[]> list = POIUtils.readExcel(excelFile);
        //用于存储 OrderSetting 对象
        List<OrderSetting> orderSettings = new ArrayList<>();

        for (String[] strings : list) {
            String orderDate = strings[0];
            String number = strings[1];

            OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
            orderSettings.add(orderSetting);
        }
        Result result = orderSettingService.add(orderSettings);
        return result;
    }


    @RequestMapping("/getOrderSettingByMonth")
    private Result getOrderSettingByMonth(String date) { // xxxx-xx
        Result result = orderSettingService.getOrderSettingByMonth(date);


        return result;
    }


    @RequestMapping("/editOrderSetting")
    private Result editOrderSetting(@RequestBody OrderSetting orderSetting) {
        Result result = orderSettingService.editOrderSetting(orderSetting);
        return result;
    }


}
